package com.jin.Es;

import com.alibaba.fastjson.JSON;
import com.jin.Es.pojo.User;
import org.apache.http.HttpHost;
import org.elasticsearch.action.DocWriteResponse;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class EsDemo02 {
    private RestHighLevelClient client;

    @Before
    public void init() {
        client = new RestHighLevelClient(RestClient.builder
                (new HttpHost("192.168.200.129", 9200, "http")));
    }

    @After
    public void close() {
        try {
            client.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 向es中插入文档
     *
     * @throws IOException io异常
     */
    @Test
    public void addDocument() throws IOException {
        // 准备文档数据
        User user = new User(1L, "alice", 23, "0", "上海市浦东新区航头镇黑马程序员");
        // 新建一个索引请求 指向user
        IndexRequest indexRequest = new IndexRequest("user");
        // 向索引请求中填充数据信息
        indexRequest.id(user.getId().toString());
        // user要转换成json数据，string格式
        indexRequest.source(JSON.toJSONString(user), XContentType.JSON);
        //发送请求
        IndexResponse indexResponse = client.index(indexRequest, RequestOptions.DEFAULT);
        System.out.println(indexResponse.getResult());
    }

    /**
     * 根据id查询文档
     */
    @Test
    public void findDocument() throws IOException {
        //创建GetRequest对象，指定索引库和id
        GetRequest user = new GetRequest("user", "1");
        GetResponse getResponse = client.get(user, RequestOptions.DEFAULT);
        //解析得到的json，反序列化
        User user1 = JSON.parseObject(getResponse.getSourceAsString(), User.class);
        System.out.println(user1);
    }

    @Test
    public void deleteDocument() throws IOException {
        //创建DeleteRequest,指定要删除的文档数据所在的索引库和id
        DeleteRequest deleteRequest = new DeleteRequest("user", "1");
        //发送请求
        DeleteResponse deleteResponse = client.delete(deleteRequest, RequestOptions.DEFAULT);

        System.out.println(deleteResponse.getResult());
    }

    @Test
    public void bulkAddDocument() throws IOException {
        //使用list模拟数据源
        List<User> users = Arrays.asList(
                new User(1L, "Rose", 18, "1", "Rose同学在传智播客学表演"),
                new User(2L, "Jack", 38, "1", "Jack同学在黑马学JavaEE"),
                new User(3L, "小红", 23, "0", "小红同学在传智播客学唱歌"),
                new User(4L, "小明", 20, "1", "小明同学在黑马学JavaSE"),
                new User(5L, "达摩", 33, "1", "达摩和尚在达摩院学唱歌"),
                new User(6L, "鲁班", 24, "1", "鲁班同学走在乡间小路上"),
                new User(7L, "孙尚香", 26, "0", "孙尚香同学想带阿斗回东吴"),
                new User(8L, "李白", 27, "1", "李白同学在山顶喝着酒唱着歌"),
                new User(9L, "甄姬", 28, "0", "甄姬同学弹奏一曲东风破"),
                new User(10L, "虞姬", 27, "0", "虞姬同学在和项羽谈情说爱"));

        //创建BulkRequest请求
        BulkRequest bulkRequest = new BulkRequest();
        //创建多个IndexRequest对象并添加到bulkRequest中
        for (User user : users) {
            bulkRequest.add(new IndexRequest("user")
                    .id(user.getId().toString())
                    .source(JSON.toJSONString(user), XContentType.JSON));
        }
        //发送请求
        BulkResponse bulkResponse = client.bulk(bulkRequest, RequestOptions.DEFAULT);
        System.out.println(bulkResponse.status());


    }


    @Test
    public void test() {
        User user = new User(1L, "alice", 23, "0", "上海市浦东新区航头镇黑马程序员");
        System.out.println(JSON.toJSON(user).getClass());
        System.out.println(JSON.toJSONString(user).getClass());
    }

}
